﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Ultility;
using ToolwebAQ.database;
using System.Collections;
using DevExpress.Web.ASPxGridView;

namespace ToolwebAQ.adminaq
{
    public partial class AQManagerMenu : System.Web.UI.Page
    {
        public int stt = 1;
        Hashtable copiedValues = null;
        string[] copiedFields = new string[] { "STT", "Tree_Name", "isused" };

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Source.obj_Account] != null)
            {
                if (Session[Source.obj_role].ToString() == "True")
                {
                    if (Session[Source.obj_role].ToString().Trim() == Source.obj_A.Trim())
                    {
                        if (!IsPostBack)
                        {

                        }
                        else
                        {
                            Response.Redirect("../adminaq/AQLogin.aspx");
                        }
                    }
                }
                else
                {
                    Response.Redirect("404.html");
                }
            }
            else
            {
                Response.Redirect("../adminaq/AQLogin.aspx");
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            BindListMenu();
        }

        private void BindListMenu()
        {
            using (aquariusDBDataContext db = new aquariusDBDataContext())
            {
                var listmenu = from menu in db.TreeDataTbls
                               join role in db.Tbl_foreign_Account_Roles
                               on menu.Tree_ID equals role.Tree_ID
                               into grp
                               from lst in grp.DefaultIfEmpty()
                               select new
                               {
                                   Tree_Name = menu.Tree_Name,
                                   coderole = lst == null ? " " : lst.coderole,
                                   Idforeign = lst == null ? lst.Idforeign.ToString() : "",
                                   Tree_ID = lst == null ? lst.Tree_ID.ToString() : "",
                                   isused = menu.isused
                               };
                SqlCommand cmd = (SqlCommand)db.GetCommand(listmenu);
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);

                grdMenu.DataSource = ds;
                grdMenu.DataBind();
            }
        }

        protected void grdMenu_CustomButtonCallback(object sender, ASPxGridViewCustomButtonCallbackEventArgs e)
        {
            if (e.ButtonID != "Delete")
                return;
            copiedValues = new Hashtable();
            foreach (string fieldName in copiedFields)
            {
                copiedValues[fieldName] = grdMenu.GetRowValues(e.VisibleIndex, fieldName);
            }
            grdMenu.AddNewRow();
        }

        protected void grdMenu_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
        {

        }
    }
}